package com.imu.utils;
import java.io.IOException;
import java.util.regex.Matcher;  
import java.util.regex.Pattern;
import com.xiaoleilu.hutool.util.StrUtil;
import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;  
  
/** 
 * 检测是否为移动端设备访问 
 * @author      : sunhongwei 
 * @Version     : 1.00 
 * @Date        : 2017-12-21 下午01:34:31 
 */
public class CheckMobile 
{  
    // \b 是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔),    
    // 字符串在编译时会被转码一次,所以是 "\\b"    
    // \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)    
    static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"    
    					   + "|windows (phone|ce)|blackberry"    
    					   + "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"    
    					   + "|laystation portable)|nokia|fennec|htc[-_]"    
    					   + "|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
    
    static String ipadReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"
            			  + "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";    
      
    //移动设备正则匹配：手机端、平板  
    static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);    
    static Pattern ipadPat  = Pattern.compile(ipadReg,  Pattern.CASE_INSENSITIVE);
    
    public static final String MOBILE = "m";
    static UASparser uasParser = null;  
    
    static
    {
        try
        {
            uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());  
        } 
        catch (IOException e){}
    }
        
    /** 
     * 检测是否是移动设备访问 
     * @Title: check 
     * @Date : 2017-12-21 下午01:29:07 
     * @param userAgent 浏览器标识 
     * @return true:移动设备接入，false:pc端接入 
     */  
    public static boolean check(String userAgent)
    {
    	if(StrUtil.isBlank(userAgent)) return false;
        
        // 匹配    
        Matcher matcherPhone = phonePat.matcher(userAgent);    
        Matcher matcherTable = ipadPat.matcher(userAgent);    
        
        if(matcherPhone.find() || matcherTable.find())
        {    
            return true;    
        }
        
        return false;
    }
    
    /** 
     * 设备终端类型 
     * @Title: agent
     * @Date : 2017-12-21 下午01:29:07 
     * @param userAgent 浏览器标识 
     * @return UserAgentInfo: 终端信息 
     */  
    public static UserAgentInfo getAgent(String userAgent)
    {    
        if(StrUtil.isBlank(userAgent)) return null;
        
    	try
    	{
			return CheckMobile.uasParser.parse(userAgent);
		} 
    	catch (IOException e) {}
    	
    	return null;
    }
}  